package com.thb.pay.db.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thb.pay.core.db.BasePage;
import com.thb.pay.core.db.Direction;
import com.thb.pay.core.db.Order;

/**
 * @Description 查询条件拼凑
 * @Author lucky
 * @data 2019/7/21 23:42
 */
public class QueryUtils {

    public static LambdaQueryWrapper buildLambQueryPage(BasePage basePage) {
        LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
        return buildLambPage(wrapper, basePage);
    }

    public static <T> LambdaQueryWrapper<T> buildLambdaQuery(Class<T> entity) {
        return new LambdaQueryWrapper();
    }
    public static QueryWrapper buildQueryPage(BasePage basePage) {
        QueryWrapper wrapper = new QueryWrapper();
        return buildQueryPage(wrapper, basePage);

    }
    public static <T> QueryWrapper<T> queryWrapper(Class<T> entity) {
        return new QueryWrapper();
    }

    public static LambdaQueryWrapper buildLambPage(LambdaQueryWrapper wrapper, BasePage basePage) {
        if (wrapper == null) {
            wrapper = new LambdaQueryWrapper();
        }
        if (basePage != null) {
            if (basePage.getOrders() != null && basePage.getOrders().length > 0) {
                for (Order order : basePage.getOrders()) {
                    if (order.getDirection().compareTo(Direction.ASC) == 0) {
                        wrapper.orderByAsc(order.getField());
                    } else {
                        wrapper.orderByDesc(order.getField());
                    }
                }
            }
        }
        return wrapper;
    }

    public static QueryWrapper buildQueryPage(QueryWrapper wrapper, BasePage basePage) {
        if (wrapper == null) {
            wrapper = new QueryWrapper();
        }
        buildPage(wrapper, basePage);
        return wrapper;
    }

    private static void buildPage(QueryWrapper wrapper, BasePage basePage) {
        if (basePage != null) {
            if (basePage.getOrders() != null && basePage.getOrders().length > 0) {
                for (Order order : basePage.getOrders()) {
                    if (order.getDirection().compareTo(Direction.ASC) == 0) {
                        wrapper.orderByAsc(order.getField());
                    } else {
                        wrapper.orderByDesc(order.getField());
                    }
                }
            }
        }
    }
}
